package com.ljfiction.book.fragment.ranking;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ljfiction.book.R;
import com.ljfiction.book.fragment.BaseFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Android on 2017/12/7.
 */

public class RankThreetatleFragment extends BaseFragment {
    @BindView(R.id.tVeek3_ranking)
    TextView tVeek3Ranking;
    @BindView(R.id.tVreader3_ranking)
    TextView tVreader3Ranking;
    @BindView(R.id.tVautho3_ranking)
    TextView tVautho3Ranking;
    @BindView(R.id.small_threefragment)
    FrameLayout smallThreefragment;
    Unbinder unbinder;
    @BindView(R.id.threabang3_ranking)
    LinearLayout threabang3Ranking;
    private int type;
    private Context context;
    private RankNotatleFragment mfragment, fragmentweek, fragmentmoth, fragmentAll;
    private FragmentTransaction transaction;
    private FragmentManager fragmentManager;

    public static RankThreetatleFragment newInstance(Context context, int type) {
        RankThreetatleFragment newFragment = new RankThreetatleFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        newFragment.setArguments(bundle);
        return newFragment;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.ranking_threetatle, container, false);
        Bundle args = getArguments();
        if (args != null) {
            this.context = getContext();
            this.type = args.getInt("type");
        }
        fragmentManager = getChildFragmentManager();

        fragmentweek = RankNotatleFragment.newInstance(context, type, 1);
        addFragment(fragmentManager.beginTransaction(), R.id.small_threefragment, fragmentweek, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void addFragment(FragmentTransaction transaction, int id, BaseFragment frament, boolean b) {
        if (b)
            transaction.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out, R.anim.back_left_in, R.anim.back_right_out);
        transaction.add(id, frament);
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
        hideFragment(transaction, fragmentAll);
        hideFragment(transaction, fragmentmoth);
        hideFragment(transaction, fragmentweek);

    }

    private void showFragment(FragmentTransaction transaction, int id, BaseFragment frament, boolean b) {
        if (b)
            transaction.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out, R.anim.back_left_in, R.anim.back_right_out);
        transaction.show(frament);
        transaction.commit();
    }

    public void hideFragment(FragmentTransaction transaction, BaseFragment frament) {
        if (frament != null) {
            transaction.hide(frament);
        }
    }

    @OnClick({R.id.tVeek3_ranking, R.id.tVreader3_ranking, R.id.tVautho3_ranking})
    public void onViewClicked(View view) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        switch (view.getId()) {
            case R.id.tVeek3_ranking:
                if (fragmentweek==null){
                    fragmentweek=RankNotatleFragment.newInstance(context,type,1);
                    addFragment(transaction, R.id.small_threefragment, fragmentweek, false);
                }else {
                    showFragment(transaction, R.id.small_threefragment, fragmentweek, false);
                }
                tVreader3Ranking.setBackgroundColor(Color.parseColor("#e7e7e7"));
                tVautho3Ranking.setBackgroundResource(R.drawable.mine_tab_right_false);
                tVeek3Ranking.setBackgroundResource(R.drawable.mine_tab_left_true);
                tVreader3Ranking.setTextColor(Color.parseColor("#333333"));
                tVeek3Ranking.setTextColor(Color.parseColor("#ffffff"));
                tVautho3Ranking.setTextColor(Color.parseColor("#333333"));
                break;
            case R.id.tVreader3_ranking:
                if (fragmentmoth==null){
                    fragmentmoth=RankNotatleFragment.newInstance(context,type,2);
                    addFragment(transaction, R.id.small_threefragment, fragmentmoth, false);
                }else {
                    showFragment(transaction, R.id.small_threefragment, fragmentmoth, false);
                }
                tVreader3Ranking.setBackgroundColor(Color.parseColor("#F77583"));
                tVautho3Ranking.setBackgroundResource(R.drawable.mine_tab_right_false);
                tVeek3Ranking.setBackgroundResource(R.drawable.mine_tab_left_false);
                tVreader3Ranking.setTextColor(Color.parseColor("#ffffff"));
                tVeek3Ranking.setTextColor(Color.parseColor("#333333"));
                tVautho3Ranking.setTextColor(Color.parseColor("#333333"));
                break;
            case R.id.tVautho3_ranking:
                if (fragmentAll==null){
                    fragmentAll=RankNotatleFragment.newInstance(context,type,3);
                    addFragment(transaction, R.id.small_threefragment, fragmentAll, false);
                }else {
                    showFragment(transaction, R.id.small_threefragment, fragmentAll, false);
                }
                tVreader3Ranking.setBackgroundColor(Color.parseColor("#e7e7e7"));
                tVautho3Ranking.setBackgroundResource(R.drawable.mine_tab_right_true);
                tVeek3Ranking.setBackgroundResource(R.drawable.mine_tab_left_false);
                tVreader3Ranking.setTextColor(Color.parseColor("#333333"));
                tVeek3Ranking.setTextColor(Color.parseColor("#333333"));
                tVautho3Ranking.setTextColor(Color.parseColor("#ffffff"));
                break;
        }
    }
}
